package cn.com.callback

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.com.elite.EliteBean;
import cn.com.elite.PropertiesUtil;
import cn.com.elite.Util;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import net.sf.json.JSONObject;

/**
 * @author ThinkPad
 */

public class erpinterface implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(erpinterface.class);

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        PropertiesUtil properties = new PropertiesUtil();
        def erpservletip = properties.readValue("erpservletip"); //获取配置文件erp接口IP
        def crmservletip = properties.readValue("crmservletip");//获取配置文件CRM接口IP
        def erpsecretkey = properties.readValue("erpsecretkey");//获取配置文件erp接口加密Key
        def sqlrequest = "";
        def sqlresult = "";
        def rownum = 0;
        def requestinfo = "";
        def url = "";
        def charset = "utf-8";
        def timeout = 100000;
        def enableSSLflag = 0;
        try {
            def receivebody = Util.getRequestBody(request, 'utf-8'); //获取报文
            JSONObject obj = JSONObject.fromObject(receivebody);
            obj.put("secretkey", erpsecretkey);
            url = crmservletip + "/design";
            def result = Util.doHttpPost(url, JSONObject.fromObject(obj).toString(), charset, timeout, enableSSLflag);
            if (result == '0') {
                return "{\"code\":\"e000002\",\"content\":\"\",\"msg\":\"\",\"rsp\":\"sign error!\",\"rspCode\":\"\",\"sub_msg\":\"签名错误!\"}"
            };
            def method = obj.get("method"); //获取接口名称
            if (method == 'update_receiptstatus') {
                url = crmservletip + "/update_receiptstatus";
                return Util.doHttpPost(url, receivebody, charset, timeout, enableSSLflag);
            }
        } catch (Exception e) {
            log.error("", e);
            return "{\"code\":\"e000005\",\"content\":\"\",\"msg\":\"\",\"rsp\":\"init interface fail\",\"rspCode\":\"\",\"sub_msg\":\"接口调用失败！\"}"
        };

    }
}